package game;

import game.classes.NetworkChannel;
import game.classes.NetworkMessage;
import game.classes.Player;
import javax.swing.JOptionPane;

/**
 * @author Chris
 */
public class Multiplayer extends Singleplayer {

    NetworkChannel net;

    public Multiplayer(Game frame, Player player) {
        super(frame, player);
        net = new NetworkChannel(this);
    }

    public void setup() {
        String opts[] = {"Unirte a una partida en red", "Crear una partida en red", "Cancelar"};

        int action = JOptionPane.showOptionDialog(game,
                "Que deseas hacer?",
                "Multiplayer",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE,
                null, opts, opts[0]);
        if (action == 0) {
            joinGame();
            play();
        } else if (action == 1) {
            createGame();
            play();
        } else {
            game.showMainMenu();
        }
    }

    private void createGame() {
        net.setupServer();
        new Thread(net).start();
    }

    private void joinGame() {
        net.setupClient("localhost");
        new Thread(net).start();
    }

    @Override
    public void notifyNetworkMessage(NetworkMessage m) {
        System.out.println(m.getPlainMessage());
    }
}
